home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* feedback.c
- * This program demonstrates use of OpenGL feedback. First,
- * a lighting environment is set up and a few primitives are
- * drawn. Then feedback mode is entered, and the same
- * primitives are drawn. The results in the feedback buffer
- * are printed.
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid drawGeometry( GLboolean );
-
- GLvoid print3DcolorVertex( GLint *, GLfloat *, GLboolean );
- GLvoid printBuffer( GLint, GLfloat * );
- GLvoid printHits( GLint, GLuint [] );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- #define BUFSIZE 1024
-
- /* Global Variables */
-
- GLboolean feedbackFlag = GL_FALSE;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- /* create a window that is 1/4 the size of the screen */
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - This program demonstrates feedback \n"
- "SPACE key - toggle feedback mode \n"
- "Escape key - exit the program \n\n",
- progname );
- }
-
-
- GLvoid
- initgfx( void )
- {
- GLfloat mat_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
- GLfloat mat_diffuse[] = { 1.0, 1.0, 0.0, 1.0 };
- GLfloat light_position[] = { 0.0, 0.707, 0.707, 0.0 };
-
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glLightfv( GL_LIGHT0, GL_POSITION, light_position);
- glEnable( GL_LIGHTING );
- glEnable( GL_LIGHT0 );
-
- glClearColor( 0, 0, 0, 1 );
- glEnable( GL_DEPTH_TEST );
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- aspect = (GLdouble) width / (GLdouble) height;
- gluPerspective( 45.0, aspect, 1.0, 20.0 );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case ' ': /* toggle feedback when the SPACE BAR is pressed */
- feedbackFlag = !feedbackFlag;
- glutPostRedisplay();
- break;
-
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
-
- /* Draw a few lines and two points, one of which will be clipped. If in
- * feedback mode, a passthrough token is issued between each object.
- */
- GLvoid
- drawGeometry( GLboolean feedback )
- {
- glBegin( GL_LINE_STRIP );
- glNormal3f( 0.0, 0.0, 1.0 );
- glVertex3f( 3.0, 3.0, -5.0 );
- glVertex3f( 3.0, 6.0, -6.0 );
- glVertex3f( 0.0, 4.0, -10.0 );
- glEnd();
-
- if (feedback) glPassThrough( 1.0 );
-
- glBegin( GL_POINTS );
- glVertex3f( -100.0, -100.0, -100.0 ); /* clipped */
- glVertex3f( 2.0, 2.0, -100.0 );
- glEnd();
-
- if (feedback) glPassThrough( 2.0 );
-
- glBegin( GL_POINTS );
- glNormal3f( 0.0, 0.0, 1.0 );
- glVertex3f( -2.0, -2.0, -2.0 );
- glEnd();
-
- if (feedback) glPassThrough( 3.0 );
-
- glutSolidCube( 1.0 );
-
- if (feedback) glPassThrough( 4.0 );
-
- glTranslatef( 2.0, 0.0, -1.0 );
- glutSolidTetrahedron();
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- GLfloat feedBuffer[BUFSIZE];
- GLint size;
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();
- glTranslatef( 0.0, 0.0, -10.0 );
-
- drawGeometry( GL_FALSE );
-
- if ( feedbackFlag )
- {
- glFeedbackBuffer( BUFSIZE, GL_3D_COLOR, feedBuffer );
- (void) glRenderMode( GL_FEEDBACK );
-
- drawGeometry( GL_TRUE );
-
- size = glRenderMode( GL_RENDER );
- printBuffer( size, feedBuffer );
- }
- glPopMatrix();
- glutSwapBuffers();
- }
-
- GLvoid
- printBuffer( GLint size, GLfloat *buffer )
- {
- GLint count = 0, i;
- GLuint token, nVertices;
-
- while (count < size)
- {
- token = (int) buffer[count++];
- switch ( token ) {
- case GL_PASS_THROUGH_TOKEN:
- printf( "\nGL_PASS_THROUGH_TOKEN\t" );
- printf( " %4.2f\n", buffer[count++] );
- break;
- case GL_POINT_TOKEN:
- printf( "\nGL_POINT_TOKEN\n" );
- print3DcolorVertex( &count, buffer, GL_TRUE );
- break;
- case GL_LINE_TOKEN:
- printf( "\nGL_LINE_TOKEN\n" );
- print3DcolorVertex( &count, buffer, GL_TRUE );
- print3DcolorVertex( &count, buffer, GL_FALSE );
- break;
- case GL_LINE_RESET_TOKEN:
- printf( "\nGL_LINE_RESET_TOKEN\n" );
- print3DcolorVertex( &count, buffer, GL_TRUE );
- print3DcolorVertex( &count, buffer, GL_FALSE );
- break;
- case GL_BITMAP_TOKEN:
- printf( "\nGL_BITMAP_TOKEN\n" );
- print3DcolorVertex( &count, buffer, GL_TRUE );
- break;
- case GL_DRAW_PIXEL_TOKEN:
- printf( "\nGL_DRAW_PIXEL_TOKEN\n" );
- print3DcolorVertex( &count, buffer, GL_TRUE );
- break;
- case GL_COPY_PIXEL_TOKEN:
- printf( "\nGL_COPY_PIXEL_TOKEN\n" );
- print3DcolorVertex( &count, buffer, GL_TRUE );
- break;
- case GL_POLYGON_TOKEN:
- printf( "\nGL_POLYGON_TOKEN " );
- nVertices = (GLuint) buffer[count++];
- printf( "numVertices = %d\n", nVertices );
- for ( i = 0; i < nVertices; i++ )
- {
- print3DcolorVertex( &count, buffer, (i==0));
- }
- break;
- default:
- printf( "Unrecognized token\n" );
- break;
- }
- }
- printf( "\nEND FRAME\n\n" );
- }
-
-
- GLvoid
- print3DcolorVertex( GLint *count, GLfloat *buffer, GLboolean printHeader )
- {
- int i;
-
- if ( printHeader )
- fprintf( stdout, " X Y Z R G B A\n" );
-
- for (i = 0; i < 7; i++)
- {
- fprintf( stdout, "%4.2f ", buffer[(*count)++] );
- }
- fprintf( stdout, "\n" );
- }
-